<?php
/**
 * @package		Joomla.Site
 * @subpackage	Templates.beez5
 * @copyright	Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access.
defined('_JEXEC') or die;

// check modules
$showRightColumn	= ($this->countModules('position-3') or $this->countModules('position-6') or $this->countModules('position-8'));
$showbottom			= ($this->countModules('position-9') or $this->countModules('position-10') or $this->countModules('position-11'));
$showleft			= ($this->countModules('position-4') or $this->countModules('position-7') or $this->countModules('position-5'));


$isHome = $this->countModules('our-brands');

if ($showRightColumn==0 and $showleft==0) {
	$showno = 0;
}

JHtml::_('behavior.framework', true);

// get params
$color			= $this->params->get('templatecolor');
$logo			= $this->params->get('logo');
$navposition	= $this->params->get('navposition');
$app			= JFactory::getApplication();
$doc			= JFactory::getDocument();
$templateparams	= $app->getTemplate(true)->params;

$doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/md_stylechanger.js', 'text/javascript', true);
?>
<!DOCTYPE html>

<html>
<head>
	<jdoc:include type="head" />
	
	<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/fonts.css">
	<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/style.css">
	<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/pml.css">
	<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/select2/select2.min.js"></script>
	<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/select2/select2.css">
	<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/jquery.bxslider.min.js"></script>
	<link href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/jquery.bxslider.css" rel="stylesheet" />	
	
	<?php if($isHome) { ?>
		<style>
			body{background: #fff url("<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/bg.jpg") no-repeat; background-size: cover;}
		</style>
	<?php } ?>
	
	<script>
		function goPage(no)
		{
			current = no;
			var start = num_per_page*(current-1);

			$('.car-listing .block-car:not(.hide)').each(function(index){
				if (index < start || index >= (start+num_per_page)) {
					$(this).addClass('outpage');
				}
				else
				{
					$(this).removeClass('outpage');
				}
			});
			
			$('.pagination ul li a').removeClass("active");
			$('.pagination ul li a[rel='+no+']').addClass("active");
		}
		
		function pagination()
		{
			var num_page = Math.ceil(total/num_per_page);
			//alert(num_page);
					
			//render
			var html = '<ul>';
			for(var i=0; i < num_page; i++) {
				html += '<li><a class="pp" href="#p" rel="'+(i+1)+'">'+(i+1)+'</a></li>';
			}
			html += '<li><a href="#p" class="prev">&lt;</a></li>'
				+ '<li><a href="#p" class="next">&gt;</a></li>'
				+ '</ul>';
			
			$('.pagination').html(html);
			
			$('.pagination ul li a.pp').on("click", function() {
				goPage(parseInt($(this).attr("rel")));
			});
			
			$('.pagination ul li a.prev').on("click", function() {
				if(current > 1) goPage(current-1);
			});
			
			$('.pagination ul li a.next').on("click", function() {
				if(current < num_page) goPage(current+1);
			});
			
			//init						
			goPage(1);
		}
		
		function filter()
		{
			//filter
			$('.car-listing .block-car').removeClass('hide');
			$('.car-listing .block-car').each(function(){
				var block = $(this);
				$('.filter .selection select').each(function() {
					if ($(this).val() != "") {
						if(block.find('.filter-item[rel='+$(this).attr("name")+']').html() != $(this).val())
						{
							block.addClass('hide');
						}
					}				
				});
			});
			
			//pagination
			total = $('.car-listing .block-car:not(.hide)').length
			pagination();
		}
		
		function unique(arr) {
			var hash = {}, result = [];
			for ( var i = 0, l = arr.length; i < l; ++i ) {
			    if ( !hash.hasOwnProperty(arr[i]) ) { //it works with objects! in FF, at least
				hash[ arr[i] ] = true;
				result.push(arr[i]);
			    }
			}
			return result;
		}
		
		var make_array = [];
		var model_array = [];
		var price_array = [];
		var year_array = [];
		var reg_date_array = [];
		var transmission_array = [];
		var status_array = [];
		var total = 0;
		var current = 1;
		var num_per_page = 10;
		$( document ).ready(function(){
			total = $('.car-listing .block-car').length;
			pagination();
			
			$('.box1 h1').html($('#content h1').eq(0).html());
			$('#content h1').eq(0).css("display","none");
			$('.bxslider').bxSlider({	
  				mode: 'fade',	
  				auto: true,
				moveSlides: 1,
				minSlides: 3,
				maxSlides: 5,	
				controls: true,
				pager: false
			});
			
			$('.view-gallery-but').on("click", function(){
				//if ($('.gallery').css("display") == "none") {
					$('.gallery_out').removeClass("hidden");
					$('.car_detail').css("display","none");
					$('.breadcrumbs').html($('.breadcrumbs').html()+"<i> / Gallery</i>");
					$('body').css("background","#222222");
				//}
				$('.breadcrumbs span').on("click", function(){
					//if ($('.gallery').css("display") == "none") {
						$('.gallery_out').addClass("hidden");
						$('.car_detail').css("display","block");
						$('.breadcrumbs i').remove();
						$('body').css("background","#fff");
					//}
				});
			});
			
			$('.gallery img').on("click", function(){
				$('.zoom-content img').removeClass('active');
				$('.zoom-content img[rel='+$(this).attr('rel')+']').addClass('active');
				$('.zoom').css("display","block");
			});
			
			$('.controls a.close').on("click", function(){
				$('.zoom').css("display","none");
			});
			
			$('.controls a.next').on("click", function(){
				if(!$('.zoom-content img.active').length)
				{
				    $('.zoom-content img').eq(0).addClass('active');
				}
				else
				{
				    if($('.zoom-content img.active').next().prop("tagName") == 'IMG')
				    {
					var a = $('.zoom-content img.active').next();
					$('.zoom-content img.active').removeClass('active');   
					a.addClass('active');         
				    }
				    else
				    {
					$('.zoom-content img.active').removeClass('active');
					$('.zoom-content img').eq(0).addClass('active');
				    }
				}
			});
			$('.controls a.prev').on("click", function(){
				if(!$('.zoom-content img.active').length)
				{
				    $('.zoom-content img').eq(0).addClass('active');
				}
				else
				{
				    if($('.zoom-content img.active').prev().prop("tagName") == 'IMG')
				    {
					var a = $('.zoom-content img.active').prev();
					$('.zoom-content img.active').removeClass('active');   
					a.addClass('active');         
				    }
				    else
				    {
					$('.zoom-content img.active').removeClass('active');
					$('.zoom-content img').eq($('.zoom-content img').length-1).addClass('active');
				    }
				}
			});
			
			$('.mitsubishi-block').html($('.mitsubishi-content').html());
			$('.kia-block').html($('.kia-content').html());
			
			//init filter			
			$('.car-listing .block-car').each(function(){
				if($(this).find('.filter-item[rel=make]').html() != '') make_array[make_array.length] = $(this).find('.filter-item[rel=make]').html();
				if($(this).find('.filter-item[rel=model]').html() != '') model_array[model_array.length] = $(this).find('.filter-item[rel=model]').html();
				if($(this).find('.filter-item[rel=price]').html() != '') price_array[price_array.length] = $(this).find('.filter-item[rel=price]').html();
				if($(this).find('.filter-item[rel=year]').html() != '') year_array[year_array.length] = $(this).find('.filter-item[rel=year]').html();
				if($(this).find('.filter-item[rel=reg_date]').html() != '') reg_date_array[reg_date_array.length] = $(this).find('.filter-item[rel=reg_date]').html();
				if($(this).find('.filter-item[rel=transmission]').html() != '') transmission_array[transmission_array.length] = $(this).find('.filter-item[rel=transmission]').html();
				if($(this).find('.filter-item[rel=status]').html() != '') status_array[status_array.length] = $(this).find('.filter-item[rel=status]').html();
				
			});
			make_array = unique(make_array.sort());
			model_array = unique(model_array.sort());
			price_array = unique(price_array.sort());
			year_array =  unique(year_array.sort());
			reg_date_array = unique(reg_date_array.sort());
			transmission_array = unique(transmission_array.sort());
			status_array = unique(status_array.sort());
			
			for (var index = 0; index < make_array.length; index++) {
				$("#f_make").append('<option value="'+make_array[index]+'">'+make_array[index]+'</option>');
			}
			for (var index = 0; index < model_array.length; index++) {
				$("#f_model").append('<option value="'+model_array[index]+'">'+model_array[index]+'</option>');
			}
			for (var index = 0; index < price_array.length; index++) {
				$("#f_price").append('<option value="'+price_array[index]+'">'+price_array[index]+'</option>');
			}
			for (var index = 0; index < year_array.length; index++) {
				$("#f_year").append('<option value="'+year_array[index]+'">'+year_array[index]+'</option>');
			}
			for (var index = 0; index < reg_date_array.length; index++) {
				$("#f_reg_date").append('<option value="'+reg_date_array[index]+'">'+reg_date_array[index]+'</option>');
			}
			for (var index = 0; index < transmission_array.length; index++) {
				$("#f_transmission").append('<option value="'+transmission_array[index]+'">'+transmission_array[index]+'</option>');
			}
			for (var index = 0; index < status_array.length; index++) {
				$("#f_status").append('<option value="'+status_array[index]+'">'+status_array[index]+'</option>');
			}
			
			$("#f_make").select2();
			$("#f_model").select2();
			$("#f_price").select2();
			$("#f_year").select2();
			$("#f_reg_date").select2();
			$("#f_transmission").select2();
			$("#f_status").select2();
			
			$('.filter .selection select').change(function() {
				filter();
			});
			
			
			
		});
	</script>
</head>
<body>
<div id="container">
	<div id="wrapper">
		<div id="header">
			<div class="logo">
				<jdoc:include type="modules" name="logo" />				
			</div>
			<div id="navbar" >
				<jdoc:include type="modules" name="navbar" />				
			</div>
		</div><!--header-->
		<div class="clear"></div>
		
		<jdoc:include type="modules" name="callus" />
		
		                                                                              
		<div class="clear"></div>
		
		<?php if(!$isHome) { ?>
			<div class="box1">
				<div class="w960">
					<h1></h1>
	
					<jdoc:include type="modules" name="breadcrumbs" />
				</div>
			</div>
			<div class="clear"></div>
		<?php } ?>
		
		<div id="content">
			<jdoc:include type="modules" name="filter-listing" />
			<jdoc:include type="message" />
			<jdoc:include type="component" />
			
			<div class="mitsubishi-content" style="display: none">
				<jdoc:include type="modules" name="mitsubishi" />
			</div>
			<div class="kia-content" style="display: none">
				<jdoc:include type="modules" name="kia" />
			</div>
			
		</div><!--content-->
		<div class="clear"></div>
		
				<jdoc:include type="modules" name="our-brands" />
				<!--<h2>OUR BRANCHS</h2>
				<ul>
					<li><a href=""><img src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/citroen.png" alt=""></a></li>
					<li><a href=""><img src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/mitsubishi.png" alt=""></a></li>
					<li><a href=""><img src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/kia.png" alt=""></a></li>
				</ul>-->

	</div><!--Wrapper-->
	<div id="footer">
		<div class="w960">
			<div id="copyright">
				<p>&copy; 2014 Fulco. All rights reserved.</p>
			</div>
			<div class="term">
				<jdoc:include type="modules" name="footer-menu" />				
			</div>
		</div>
	</div><!--footer-->
</div>
</body>
</html>